package com.gzx.risk.core.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

public class DataConvertUtils {
    /**
     * 将 byte[] 转换为 Base64 编码字符串（不含前缀）
     * @param bytes 字节数组（由 inputStreamToBytes 方法生成）
     * @return Base64 编码字符串
     */
    public static String bytesToBase64(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            throw new IllegalArgumentException("字节数组不能为空");
        }
        return Base64.getEncoder().encodeToString(bytes);
    }

    // 辅助方法：将 InputStream 转换为字节数组
    public static byte[] inputStreamToBytes(InputStream is) throws IOException {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            return os.toByteArray();
        } finally {
            is.close(); // 关闭原始流
        }
    }
}